<template>
	<view class="mainpadding">
		<view class="mainpadding ffffff radius">
			<textarea placeholder="请输入内容~" style="width: 100%;height: 200rpx;" v-model="des"></textarea>
			<view class="flexleft flex_wrap">
				<view class="shilitu margin_top margin_right2 dingwei" v-for="(item,index) in images_text" :key="index">
					<image :src="item" mode=""></image>
					<view class="shanchu" @click="delindex(index,images_text,images)">
						<u-icon name="close-circle-fill"></u-icon>
					</view>
				</view>
				<view class="margin_top shangc margin_right2" v-if="images_text.length<5"
					@click="uploadImg(5,images,images_text)">
					<image src="../static/image/system/shangchuan.png" mode=""></image>
				</view>
			</view>
		</view>
		<view class="gudingdb ffffff mainpadding">
			<view class="bigbtn" @click="submit">提交</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")

	export default {
		data() {
			return {
				des: "",
				images: [],
				images_text: [],
				cg_kh_id:"",
				listid:"",//编辑id
			}
		},
		onLoad(options) {
			this.cg_kh_id = options.cg_kh_id
			if (options.id) {
				this.listid = options.id
				this.getDetail()
				uni.setNavigationBarTitle({
					title:"编辑记录"
				})
			}
		},
		methods: {
			getDetail(){
				let data = {
					id:this.listid
				}
				let url = "/api/user/cgKhRecordDetail"
				httpRequest.request(url, 'GET', data, false, false, true).then(res => {
					this.des = res.data.des
					this.images_text = res.data.images_text
					this.images = res.data.images.split(',')
				})
			},
			submit(){
				if(this.des==""){
					httpRequest.toast("请输入内容！")
					return false
				}
				if(this.images.length==0){
					httpRequest.toast("请上传图片!")
					return false
				}
				let data = {
					des:this.des,
					images:this.images.join(),
					cg_kh_id:this.cg_kh_id,
				}
				let url = "/api/user/addCgKhRecord"
				if(this.listid){
					url = "/api/user/editCgKhRecord"
					delete data.cg_kh_id
					this.$set(data,"id",this.listid)
				}
				httpRequest.request(url, 'POST', data, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if (res.code == 1) {
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				})
				
			},
			uploadImg(value, upImgs, upImgs_text) { //上传图片
				let _this = this;
				let num = Number(value) - upImgs.length;
				if (num <= 0) {
					return false;
				}
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(num, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(num, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs, upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			delindex(index, upImg, upImgs_text) {
				upImg.splice(index, 1)
				upImgs_text.splice(index, 1)
			},
			uploadImageService(url, upImg, upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					upImg.push(res.url)
					upImgs_text.push(image_item);
				}).catch(err => {});
			},
		}
	}
</script>

<style lang="scss" scoped>
	page{
		background-color: #f9f9f9;
	}
	.shangc {
		width: 160rpx;
		height: 160rpx;
	
		image {
			width: 100%;
			height: 100%;
		}
	}
	.shanchu {
		position: absolute;
		top: -10rpx;
		right: -10rpx;
	}
	
	.shanchu2 {
		position: absolute;
		bottom: -10rpx;
		right: -50%;
		transform: translate(0, -50%);
	}
	
	.shilitu {
		width: 160rpx;
		height: 160rpx;
	
		image {
			width: 100%;
			height: 100%;
		}
	}
</style>